//
//  HZOrderDownView.m
//  Stock_Stategy
//
//  Created by MayerF on 2018/1/23.
//  Copyright © 2018年 MayerF. All rights reserved.
//

#import "HZOrderDownView.h"

#define kUpDownMargin 5
#define kLeftRightMargin 10

@interface HZOrderDownView()
@property (nonatomic, strong)UILabel *pTotalMoneyLabel;
@property (nonatomic, strong)UILabel *pAlertLabel;
@property (nonatomic, strong)UILabel *pDelay;

@property (nonatomic, strong)NSMutableArray *pNameArray;
@property (nonatomic, strong)NSArray *pContentArray;
@end

@implementation HZOrderDownView

#pragma mark -
#pragma mark init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self p_commonInit];
    }
    return self;
}
- (void)p_commonInit
{
    _pDelay = [UILabel new];
    _pDelay.text = @"(非交易日顺延)";
    _pDelay.font = [UIFont systemFontOfSize:11];
    _pDelay.textAlignment = NSTextAlignmentRight;
    
    _pNameArray = [NSMutableArray arrayWithCapacity:7];

    
    NSArray *pTempArray = @[@"策略标的",@"策略执行金",@"履约保证金",@"综合服务费"];
    [self p_setDefaultViewWith:pTempArray];
}
- (void)p_initConstraints
{
    [_pTotalMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(kLeftRightMargin);
        make.bottom.mas_equalTo(_pAlertLabel.mas_top);
        make.right.mas_equalTo(self).offset(-kLeftRightMargin);
        make.height.mas_equalTo(30);
    }];
    [_pAlertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(kLeftRightMargin);
        make.right.mas_equalTo(self).offset(-kLeftRightMargin);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(30);
    }];
}
- (void)p_setDefaultViewWith:(NSArray *)names
{
    [self.pNameArray removeAllObjects];
    UIView *pPreObject = nil;
    NSMutableArray *pTempArray = [NSMutableArray arrayWithCapacity:7];
    for (int i = 0; i < names.count; i++) {
        NSString *name = names[i];
        UILabel *label = [UILabel new];
        UILabel *content = [UILabel new];
        label.text = name;
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = kMYHexToRGB(0x999999, 1);
        content.textAlignment = NSTextAlignmentRight;
        label.font = [UIFont systemFontOfSize:15];
        content.font = [UIFont systemFontOfSize:15];
        content.text = @"--";
        
        [self addSubview:label];
        [self addSubview:content];
        [self.pNameArray addObject:label];
        if (i == 0) {
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self);
                make.left.mas_equalTo(self).offset(kLeftRightMargin);
                make.right.mas_equalTo(content.mas_left);
                make.height.mas_equalTo(30);
            }];
        }else if (i == 5) {
            UIView *line = [UIView new];
            [self addSubview:line];
            line.backgroundColor = kMYHexToRGB(0xf5f5f5, 1);
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(pPreObject.mas_bottom).offset(10);
                make.left.right.mas_equalTo(self);
                make.height.mas_equalTo(1);
            }];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(pPreObject.mas_bottom).offset(20);
                make.left.mas_equalTo(self).offset(kLeftRightMargin);
                make.right.mas_equalTo(content.mas_left);
                make.height.mas_equalTo(30);
            }];
        }else {
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(pPreObject.mas_bottom);
                make.left.mas_equalTo(self).offset(kLeftRightMargin);
                make.right.mas_equalTo(content.mas_left);
                make.height.mas_equalTo(30);
            }];
            if (i == 4) {
                [content addSubview:_pDelay];
                [_pDelay mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.width.mas_equalTo(content);
                    make.centerY.mas_equalTo(content).mas_offset(15);
                    make.height.mas_equalTo(20);
                }];
            }
        }
        [content mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(label);
            make.right.mas_equalTo(self).offset(-kLeftRightMargin);
            make.width.mas_equalTo(label);
            make.height.mas_equalTo(label);
        }];
        
        pPreObject = label;
        [pTempArray addObject:content];
    }
    
    _pContentArray = pTempArray;
    
    [pPreObject mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self).offset(-10);
    }];
}

- (void)updateNewData:(NSArray *)data
{
    for (int i = 0; i < _pContentArray.count; i++) {
        UILabel *label = _pContentArray[i];
        NSString *text = data[i];
        label.text = text;
        if (i == 7) {
            label.textColor = kREDColor;
            label.font = [UIFont boldSystemFontOfSize:15];
        }
    }
}

- (void)updateName:(NSArray *)names
{
    if (names.count == _pNameArray.count) {
        [_pNameArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UILabel *label = obj;
            label.text = names[idx];
        }];
    }
}

@end
